package user

import (
	"github.com/gin-gonic/gin"

	"github.com/lab-online/pkg/auth"

	"github.com/lab-online/internal/user/domain"
	"github.com/lab-online/internal/user/export"
	"github.com/lab-online/internal/user/handler"
	"github.com/lab-online/internal/user/infra/repository"
	"github.com/lab-online/internal/user/interface/rest"
)

var (
	NewRepository = repository.New
	NewDomain     = domain.New
	NewExport     = export.New
)

type Route struct {
	rest.UserHandler
	auth auth.Middleware
}

func NewRoute(authMiddleware auth.Middleware, d domain.UserDomain) *Route {
	h := handler.New(d)
	return &Route{h, authMiddleware}
}

func (r *Route) Register(router *gin.RouterGroup) {
	authTeacherOrAdmin := r.auth(auth.RoleTeacher | auth.RoleAdmin)

	user := router.Group("/users")

	user.GET("", r.auth(auth.RoleAll), handler.SearchUserValidator, r.SearchUser)
	user.POST("", authTeacherOrAdmin, handler.RegisterValidator, r.UserHandler.Register)

	user.GET("/profile", r.auth(auth.RoleAll), r.GetProfile)
	user.POST("/import", authTeacherOrAdmin, handler.ImportStudentValidator, r.ImportStudent)
	user.POST("/login", handler.LoginValidator, r.Login)
	user.POST("/refresh", handler.RefreshTokenValidator, r.RefreshToken)

	user.DELETE("/:id", r.auth(auth.RoleAdmin), handler.DeleteUserValidator, r.DeleteUser)
	user.PATCH("/:id", r.auth(auth.RoleAdmin), handler.UpdateUserValidator, r.UpdateUser)

	user.PATCH("/:id/password", r.auth(auth.RoleAll), handler.UpdatePwdValidator, r.UpdatePassword)
}
